package com.example.asa;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AnnouncementsServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置字符编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        // 验证用户角色权限
        HttpSession session = request.getSession();
        String role = (String) session.getAttribute("role");

        if (!"admin".equals(role)) {
            request.setAttribute("message", "权限不足，请先以 admin 身份登录");
            RequestDispatcher dispatcher = request.getRequestDispatcher("/login.jsp");
            dispatcher.forward(request, response);
            return;
        }

        // 获取灾情ID
        String idStr = request.getParameter("id");
        if (idStr == null || idStr.isEmpty()) {
            request.setAttribute("message", "缺少灾情ID");
            RequestDispatcher dispatcher = request.getRequestDispatcher("/error.jsp");
            dispatcher.forward(request, response);
            return;
        }

        int disasterId = Integer.parseInt(idStr);

        // 数据库操作 - 删除灾情信息
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = MySQLDatabaseConnector.getConnection();
            if (conn == null) {
                System.err.println("Error: 数据库连接为空");
                request.setAttribute("message", "数据库连接失败");
                RequestDispatcher dispatcher = request.getRequestDispatcher("/error.jsp");
                dispatcher.forward(request, response);
                return;
            }

            String sql = "DELETE FROM disasters WHERE id = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, disasterId);

            int rowsDeleted = stmt.executeUpdate();
            if (rowsDeleted > 0) {
                request.setAttribute("message", "灾情信息删除成功!");
            } else {
                request.setAttribute("message", "灾情信息删除失败，请重试");
            }

            // 重定向回灾情列表页面
            response.sendRedirect(request.getContextPath() + "/disasters");

        } catch (SQLException e) {
            System.err.println("Error deleting disaster: " + e.getMessage());
            e.printStackTrace();
            request.setAttribute("message", "删除灾情信息时发生错误");
            RequestDispatcher dispatcher = request.getRequestDispatcher("/error.jsp");
            dispatcher.forward(request, response);
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
